package aynu.oracle.video.util;

import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/*
*  获取视频的信息，
*  比如 时长，帧速，码率等
* */
@Slf4j
@Component
public class VideoUtil {

    /**
     * 调用获取视频时长的方法，之后进行转换
     * @param file 文件，用于获取指定信息
     * @return
     */
    public static String getVideoTime(File file){
        String time = "";
        try{
            long ls = VideoUtil.getVideoDuration(file);
            Integer hour = (int)(ls/3600);
            Integer minute = (int)(ls%3600)/60;
            Integer second = (int)(ls-hour*3600-minute*60);
            if(minute>0){
                time += hour+":"+minute+":"+second;
            }else if(minute<0){
                time += minute+":"+second;
            }
            System.out.println(hour+":"+minute+":"+second);
        }catch (Exception e){
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 获取视频的时长
     * @param video 获取该视频的时长
     * @return
     */
    public static long getVideoDuration(File video) {
        long duration = 0L;
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
        try {
            ff.start();
            duration = ff.getLengthInTime() / (1000 * 1000);
            ff.stop();
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        }
        return duration;
    }


    /**
     * 截取视频获得指定帧的图片
     *
     * @param video   源视频文件
     * @param picPath 截图存放路径
     */
    public static void getVideoPic(File video, String picPath) {

        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
        try {
            ff.start();

            // 截取某一帧图片(具体依实际情况而定)
            int i = 0;
            int length = ff.getLengthInFrames();
            int middleFrame;

            // 根据视频的帧长，截取对应的图片
            if(length<=10000) middleFrame = length / 2; // 7分钟以内
            else if(length<=25000) middleFrame = length / 7;    // 17分钟以内
            else if(length<=40000) middleFrame = length / 12;   // 27分钟以内
            else if(length<=70000) middleFrame = length / 18;   // 47分钟以内
            else if(length<=90000) middleFrame = length / 22;   // 60分钟以内
            else if(length<=135000) middleFrame = length / 31;  // 90分钟以内
            else middleFrame = length / 41;

            System.out.println("视频帧长 = "+length+"，截取帧数 = "+middleFrame);

            Frame frame = null;
            while (i < length) {
                frame = ff.grabFrame();
                if ((i > middleFrame) && (frame.image != null)) {
                    break;
                }
                i++;
            }

            // 截取的帧图片
            Java2DFrameConverter converter = new Java2DFrameConverter();
            BufferedImage srcImage = converter.getBufferedImage(frame);
            int srcImageWidth = srcImage.getWidth();
            int srcImageHeight = srcImage.getHeight();

            // 对截图进行等比例缩放(缩略图)
            int width = 480;
            int height = (int) (((double) width / srcImageWidth) * srcImageHeight);
            BufferedImage thumbnailImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            thumbnailImage.getGraphics().drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

            File picFile = new File(picPath);
            ImageIO.write(thumbnailImage, "jpg", picFile);

            ff.stop();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getNowDate(){

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");

        Date date = new Date(System.currentTimeMillis());
//        System.out.println(formatter.format(date));

        return formatter.format(date);
    }

}